package com.foreverwin.mes.production.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.Certification;
import com.foreverwin.mes.meapi.model.Resrce;
import com.foreverwin.mes.production.model.Station;
import com.foreverwin.mes.production.service.StationCertificationService;
import com.foreverwin.mes.production.service.StationResourceService;
import com.foreverwin.mes.production.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author syngna
 * @岗位维护
 * @since 2018-11-28
 */
@RestController
@RequestMapping("/stations")
public class StationController {

    @Autowired
    private StationService stationService;
    @Autowired
    private StationResourceService stationResourceService;
    @Autowired
    private StationCertificationService stationCertificationService;


    /**
     * 查询所有岗位数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getStationList() {

        List<Station> result = new ArrayList<Station>();
        try {

            String site = CommonMethods.getSite();
            result = stationService.selectList(site);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询岗位信息
     * 以及分配的证明
     * 和分配的设备
     *
     * @param station
     * @return
     */
    @ResponseBody
    @GetMapping("/getStationAndRC")
    public AjaxResult getStationAndCertification(String station) {
        Map<String, Object> result = new HashMap<String, Object>();
        String site = CommonMethods.getSite();
        try {

            Station stationModel = stationService.getStation(site, station);
            if (stationModel == null) {
                throw BusinessException.build("岗位：" + station + "不存在");
            }

            String stationBo = stationModel.getHandle();
            //查询维护的设备
            List<Resrce> resourceList = stationResourceService.getResourceList(stationBo);
            //查询维护的证明
            List<Certification> certificationList = stationCertificationService.getCertificationList(stationBo);

            result.put("STATION", stationModel);
            result.put("RESOURCE", resourceList);
            result.put("CERTIFICATION", certificationList);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }


    /**
     * 保存岗位以及分配的证明和设备
     *
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "doSave", method = RequestMethod.POST)
    public AjaxResult stationCertificationSave(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();

        try {

            //参数
            String station = null;
            String description = null;
            String status = null;
            String modifiedDateTime = null;

            Map<String, Object> map = (Map<String, Object>) jsonObject.get("station");
            if (map == null) {
                throw BusinessException.build("参数传递错误");
            }
            if (!map.containsKey("station") || StringUtils.isBlank(map.get("station") + "")) {
                throw BusinessException.build("岗位编号不能为空");
            }
            station = (String) map.get("station");

            if (!map.containsKey("status") || StringUtils.isBlank(map.get("status") + "")) {
                throw BusinessException.build("岗位状态不能为空");
            }
            status = (String) map.get("status");

            if (map.containsKey("description")) {
                description = (String) map.get("description");
            }
            if (map.containsKey("modifiedDateTime")) {
                modifiedDateTime = (String) map.get("modifiedDateTime");
            }

            /**
             * 岗位主表数据组装
             */
            Station stationModel = new Station();
            stationModel.setStation(station);
            stationModel.setSite(site);
            stationModel.setDescription(description);
            stationModel.setStatus(status);


            List<Map> resourceList = (List<Map>) jsonObject.get("resourceList");
            List<Map> certificationList = (List<Map>) jsonObject.get("certificationList");

            stationService.stationSave(site, user, modifiedDateTime, stationModel, certificationList, resourceList);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success("岗位维护成功");
    }

    /**
     * 删除岗位以及分配的证明
     *
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "doDelete", method = RequestMethod.POST)
    public AjaxResult stationDelete(@RequestBody JSONObject jsonObject) {
        String result = "";
        String site = CommonMethods.getSite();

        try {
            String station = jsonObject.getString("station");
            if (StringUtils.isBlank(station)) {
                throw BusinessException.build("岗位不能为空");
            }

            int count = stationService.stationDelete(site, station);
            result = count > 0 ? "删除成功" : "删除失败";

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success(result);
    }

}